home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / reuse.lha / reuse / m2c / StringMem.c < prev    next >
C/C++ Source or Header  |  1992-08-18  |  4KB  |  207 lines

  1. #include "SYSTEM_.h"
  2.  
  3. #ifndef DEFINITION_DynArray
  4. #include "DynArray.h"
  5. #endif
  6.  
  7. #ifndef DEFINITION_Strings
  8. #include "Strings.h"
  9. #endif
  10.  
  11. #ifndef DEFINITION_IO
  12. #include "IO.h"
  13. #endif
  14.  
  15. #ifndef DEFINITION_StringMem
  16. #include "StringMem.h"
  17. #endif
  18.  
  19.  
  20. #define InitialMemorySize    (1024 * 16)
  21. typedef struct S_1 {
  22.     CHAR A[100000000 + 1];
  23. } Memory;
  24. static Memory *MemoryPtr;
  25. static LONGINT MemorySize;
  26. static LONGINT MemorySpaceLeft;
  27. static LONGINT MemoryFreePtr;
  28.  
  29.  
  30. StringMem_tStringRef StringMem_PutString
  31. # ifdef __STDC__
  32. (Strings_tString *s)
  33. # else
  34. (s)
  35. Strings_tString *s;
  36. # endif
  37. {
  38.   LONGINT NeededSpace;
  39.   LONGINT OldMemorySize;
  40.   LONGINT StartPtr;
  41.   Strings_tStringIndex i;
  42.  
  43.   NeededSpace = s->Length + 2;
  44.   while (MemorySpaceLeft < NeededSpace) {
  45.     OldMemorySize = MemorySize;
  46.     DynArray_ExtendArray((ADDRESS *)&MemoryPtr, &MemorySize, (LONGINT)sizeof(CHAR));
  47.     INC1(MemorySpaceLeft, MemorySize - OldMemorySize);
  48.   }
  49.   StartPtr = MemoryFreePtr;
  50.   MemoryPtr->A[MemoryFreePtr] = CHR(s->Length / 256);
  51.   INC(MemoryFreePtr);
  52.   MemoryPtr->A[MemoryFreePtr] = CHR(s->Length % 256);
  53.   INC(MemoryFreePtr);
  54.   {
  55.     Strings_tStringIndex B_1 = 1, B_2 = s->Length;
  56.  
  57.     if (B_1 <= B_2)
  58.       for (i = B_1;; i += 1) {
  59.         MemoryPtr->A[MemoryFreePtr] = s->Chars.A[i];
  60.         INC(MemoryFreePtr);
  61.         if (i >= B_2) break;
  62.       }
  63.   }
  64.   DEC1(MemorySpaceLeft, NeededSpace);
  65.   return StartPtr;
  66. }
  67.  
  68. void StringMem_GetString
  69. # ifdef __STDC__
  70. (StringMem_tStringRef r, Strings_tString *s)
  71. # else
  72. (r, s)
  73. StringMem_tStringRef r;
  74. Strings_tString *s;
  75. # endif
  76. {
  77.   Strings_tStringIndex i;
  78.  
  79.   s->Length = StringMem_Length(r);
  80.   INC1(r, 2);
  81.   {
  82.     Strings_tStringIndex B_3 = 1, B_4 = s->Length;
  83.  
  84.     if (B_3 <= B_4)
  85.       for (i = B_3;; i += 1) {
  86.         s->Chars.A[i] = MemoryPtr->A[r];
  87.         INC(r);
  88.         if (i >= B_4) break;
  89.       }
  90.   }
  91. }
  92.  
  93. CARDINAL StringMem_Length
  94. # ifdef __STDC__
  95. (StringMem_tStringRef r)
  96. # else
  97. (r)
  98. StringMem_tStringRef r;
  99. # endif
  100. {
  101.   return ORD(MemoryPtr->A[r]) * 256 + ORD(MemoryPtr->A[r + 1]);
  102. }
  103.  
  104. BOOLEAN StringMem_IsEqual
  105. # ifdef __STDC__
  106. (StringMem_tStringRef r, Strings_tString *s)
  107. # else
  108. (r, s)
  109. StringMem_tStringRef r;
  110. Strings_tString *s;
  111. # endif
  112. {
  113.   Strings_tStringIndex i;
  114.  
  115.   INC1(r, 2);
  116.   {
  117.     Strings_tStringIndex B_5 = 1, B_6 = s->Length;
  118.  
  119.     if (B_5 <= B_6)
  120.       for (i = B_5;; i += 1) {
  121.         if (MemoryPtr->A[r] != s->Chars.A[i]) {
  122.           return FALSE;
  123.         }
  124.         INC(r);
  125.         if (i >= B_6) break;
  126.       }
  127.   }
  128.   return TRUE;
  129. }
  130.  
  131. void StringMem_WriteString
  132. # ifdef __STDC__
  133. (IO_tFile f, StringMem_tStringRef r)
  134. # else
  135. (f, r)
  136. IO_tFile f;
  137. StringMem_tStringRef r;
  138. # endif
  139. {
  140.   StringMem_tStringRef i;
  141.  
  142.   {
  143.     LONGINT B_7 = r + 2, B_8 = r + 1 + (StringMem_tStringRef)StringMem_Length(r);
  144.  
  145.     if (B_7 <= B_8)
  146.       for (i = B_7;; i += 1) {
  147.         IO_WriteC(f, MemoryPtr->A[i]);
  148.         if (i >= B_8) break;
  149.       }
  150.   }
  151. }
  152.  
  153. void StringMem_WriteStringMemory
  154. # ifdef __STDC__
  155. ()
  156. # else
  157. ()
  158. # endif
  159. {
  160.   LONGINT StringPtr;
  161.   LONGINT sLength;
  162.  
  163.   StringPtr = 0;
  164.   while (StringPtr < MemoryFreePtr) {
  165.     IO_WriteI((System_tFile)IO_StdOutput, StringPtr, 5L);
  166.     IO_WriteC((System_tFile)IO_StdOutput, ' ');
  167.     StringMem_WriteString((System_tFile)IO_StdOutput, StringPtr);
  168.     IO_WriteNl((System_tFile)IO_StdOutput);
  169.     sLength = StringMem_Length(StringPtr) + 2;
  170.     INC1(StringPtr, sLength);
  171.   }
  172.   IO_WriteNl((System_tFile)IO_StdOutput);
  173.   IO_WriteI((System_tFile)IO_StdOutput, StringPtr, 5L);
  174.   IO_WriteS((System_tFile)IO_StdOutput, (STRING)" Bytes", 6L);
  175.   IO_WriteNl((System_tFile)IO_StdOutput);
  176. }
  177.  
  178. void StringMem_InitStringMemory
  179. # ifdef __STDC__
  180. ()
  181. # else
  182. ()
  183. # endif
  184. {
  185.   MemorySpaceLeft = MemorySize;
  186.   MemoryFreePtr = 0;
  187. }
  188.  
  189. void BEGIN_StringMem()
  190. {
  191.   static BOOLEAN has_been_called = FALSE;
  192.  
  193.   if (!has_been_called) {
  194.     has_been_called = TRUE;
  195.  
  196.     BEGIN_IO();
  197.     BEGIN_Strings();
  198.     BEGIN_DynArray();
  199.     BEGIN_Strings();
  200.     BEGIN_IO();
  201.  
  202.     MemorySize = InitialMemorySize;
  203.     DynArray_MakeArray((ADDRESS *)&MemoryPtr, &MemorySize, (LONGINT)sizeof(CHAR));
  204.     StringMem_InitStringMemory();
  205.   }
  206. }
  207.